home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_02 / inherit1.cpp < prev    next >
C/C++ Source or Header  |  1992-01-18  |  3KB  |  124 lines

  1.                                   // Chapter 8 - Program 1
  2. #include <iostream.h>
  3.  
  4. class vehicle {
  5. protected:
  6.    int wheels;
  7.    float weight;
  8. public:
  9.    void initialize(int in_wheels, float in_weight);
  10.    int get_wheels(void) {return wheels;}
  11.    float get_weight(void) {return weight;}
  12.    float wheel_loading(void) {return weight/wheels;}
  13. };
  14.  
  15.  
  16.  
  17.  
  18. class car : public vehicle {
  19.    int passenger_load;
  20. public:
  21.    void initialize(int in_wheels, float in_weight, int people = 4);
  22.    int passengers(void) {return passenger_load;}
  23. };
  24.  
  25.  
  26.  
  27.  
  28. class truck : public vehicle {
  29.    int passenger_load;
  30.    float payload;
  31. public:
  32.    void init_truck(int how_many = 2, float max_load = 24000.0);
  33.    float efficiency(void);
  34.    int passengers(void) {return passenger_load;}
  35. };
  36.  
  37.  
  38.  
  39.  
  40. main()
  41. {
  42. vehicle unicycle;
  43.  
  44.    unicycle.initialize(1, 12.5);
  45.    cout << "The unicycle has " <<
  46.                                 unicycle.get_wheels() << " wheel.\n";
  47.    cout << "The unicycle's wheel loading is " <<
  48.          unicycle.wheel_loading() << " pounds on the single tire.\n";
  49.    cout << "The unicycle weighs " <<
  50.                              unicycle.get_weight() << " pounds.\n\n";
  51.  
  52. car sedan;
  53.  
  54.    sedan.initialize(4, 3500.0, 5);
  55.    cout << "The sedan carries " << sedan.passengers() <<
  56.                                                     " passengers.\n";
  57.    cout << "The sedan weighs " << sedan.get_weight() << " pounds.\n";
  58.    cout << "The sedan's wheel loading is " <<
  59.                     sedan.wheel_loading() << " pounds per tire.\n\n";
  60.  
  61. truck semi;
  62.  
  63.    semi.initialize(18, 12500.0);
  64.    semi.init_truck(1, 33675.0);
  65.    cout << "The semi weighs " << semi.get_weight() << " pounds.\n";
  66.    cout << "The semi's efficiency is " <<
  67.                           100.0 * semi.efficiency() << " percent.\n";
  68. }
  69.  
  70.  
  71.  
  72.  
  73.               // initialize to any data desired
  74. void
  75. vehicle::initialize(int in_wheels, float in_weight)
  76. {
  77.    wheels = in_wheels;
  78.    weight = in_weight;
  79. }
  80.  
  81.  
  82.  
  83. void
  84. car::initialize(int in_wheels, float in_weight, int people)
  85. {
  86.    passenger_load = people;
  87.    wheels = in_wheels;
  88.    weight = in_weight;
  89. }
  90.  
  91.  
  92.  
  93.  
  94. void
  95. truck::init_truck(int how_many, float max_load)
  96. {
  97.    passenger_load = how_many;
  98.    payload = max_load;
  99. }
  100.  
  101.  
  102.  
  103. float
  104. truck::efficiency(void)
  105. {
  106.    return payload / (payload + weight);
  107. }
  108.  
  109.  
  110.  
  111.  
  112. // Result of execution
  113. //
  114. // The unicycle has 1 wheel.
  115. // The unicycle's wheel loading is 12.5 pounds on the single tire.
  116. // The unicycle weighs 12.5 pounds.
  117. //
  118. // The sedan carries 5 passengers.
  119. // The sedan weighs 3500 pounds.
  120. // The sedan's wheel loading is 875 pounds per tire.
  121. //
  122. // The semi weighs 12500 pounds.
  123. // The semi's efficiency is 72.929072 percent.
  124.